$(function(){
	//全选框选择
    $('.checkAll').click(function(){
        if($(this).is(':checked')){
            $('.content_table .checkinput').attr('checked','checked');
            $(this).attr('title','取消全选')
        }else{
            $('.content_table .checkinput').removeAttr('checked');
            $(this).attr('title','全选')
        }
    })
    //1 绑定添加按钮
    $('.add_btn').click(function(){
        layer.open({
        	extend:['skin/layer_iframe.css'],
		            skin:'layer-ext-skin-layer_iframe.css',
            type:2,
            title:'添加角色页面',
            area:['600px','400px'],
            content:'initEditSysRole.do?opera=add',
            maxmin: true,
            shadeClose: true
        });
    });
    
    //4 绑定查询按钮
    $('.sel_btn').click(function(){
    	paging(1);
    });
    
    //5 绑定重置按钮
    $('.reset_btn').click(function(){
    	$("#roleName").val('');
    	$("#roleName").focus();
    });
    //7.删除选定的，多项删除
    $('.del_select_btn').click(function(){
    	del_select_func(this);
    });
    
    //ajax初始化数据
    paging(1);
});

//修改页面
edit_func=function(obj){
    var roleId=$(obj).parent().parent().attr('roleid');
    layer.open({
    	extend:['skin/layer_iframe.css'],
		            skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'修改角色页面',
        area:['600px','400px'],
        content:'initEditSysRole.do?opera=edit&roleId='+roleId,
        maxmin: true,
        shadeClose: true
    });
}

//删除单个角色
delete_func=function(obj){
    var roleId=$(obj).parent().parent().attr('roleid');
    if(confirm('确定删除该角色和其拥有的权限?')){
        $.ajax({
            url:"deleteRoleByRoleId.do",
            type:"post",
            dataType: 'json',
            data:{
                "roleId":roleId
            },
            beforeSend:function(){
                $(obj).attr("disabled","disabled");
            },
            error:function(data){
            	layer.msg('出错了！！',{time:2000});
            },
            success:function(result){
                if(result.data){
                    layer.msg('删除成功', {time:1300});
                    paging(1);
                    $('.checkAll').removeAttr('checked');
                }else{
                    layer.msg('删除失败！', {time:1000});
                    $(obj).removeAttr("disabled");
                }
            }
        });
    }
}


//修改权限
pri_func=function(obj){
    var roleId=$(obj).parent().parent().attr('roleid');
    layer.open({
    	extend:['skin/layer_iframe.css'],
		skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'修改权限页面',
        area:['600px','400px'],
        content:'initLookSysRolePrivilege.do?roleId='+roleId,
        maxmin: true,
        shadeClose: true
    });
}

//多选删除
function del_select_func(obj){
	var checkedDel = "";
	 var arr=document.getElementsByName("checkradio");
	 	
	 	for(i=0;i<arr.length;i++){
			if(arr[i].checked){
			var a = arr[i].value;
			checkedDel+=a+",";
			}
		}
		if(checkedDel==""){
			layer.msg('先勾选选择框，再点击删除按钮！', {time:1300});
		}else{
			if(confirm('确定删除所有选中的角色和其拥有的权限?')){
			$.ajax({
            url:"deleteSelectSysRole.do",
            type:"post",
            dataType: 'json',
            data:{
                "checkedDel":checkedDel
            },
            beforeSend:function(){
                
            },
            error:function(data){
            	layer.msg('出错了！！',{time:2000});
            },
            success:function(result){
                if(result.data){
                    layer.msg('删除成功', {time:1300});
                    $('.checkAll').removeAttr('checked');
                    paging(1);
                }else{
                    layer.msg('删除失败！', {time:1000});
                    $(obj).removeAttr("disabled");
                }
            }
        });
			}
		}
}


//分页，初始化数据
function paging(pageNo) {
	var pageSize = $("#pageSize  option:selected").text();
    var roleName = $("#roleName").val();

    $.ajax({
        url: "pagingRoleList.do",
        type: "post",
        dataType: 'json',
        data: {
        	"iPageSize": pageSize,
            "iPageNo": pageNo,
            "roleName": roleName
        },
        error: function (data) {
        	layer.msg('出错了！！',{time:2000});
        },
        success: function (jsons) {
            if (jsons != null) {
                $("#pageTable").html("");
                var pageList = jsons.pageData.pageList;
                for (var i = 0; i < pageList.length; i++) {
                    var partsForm =
                        '<tr roleid="'+pageList[i].roleId +'">' +
                        '<td class="checkNot"><input type="checkbox" value="' + pageList[i].roleId + '" class="checkinput" name="checkradio" /></td>' +
                        //'<td title=' + (pageList[i].roleId == null ? '' : pageList[i].roleId) + '>' + (pageList[i].roleId == null ? '' : pageList[i].roleId) + '</td>' +
                        '<td title=' + (pageList[i].roleName == null ? '' : pageList[i].roleName) + '>' + (pageList[i].roleName == null ? '' : pageList[i].roleName) + '</td>' +
                        '<td title=' + (pageList[i].state == "9ia" ? "启用" : "关闭") + '>' + (pageList[i].state == "9ia" ? "启用" : "关闭") + '</td>' +
                        '<td title=' + (pageList[i].remark == null ? '' : pageList[i].remark) + '>' + (pageList[i].remark == null ? '' : pageList[i].remark) + '</td>' +
                        '<td><i class="Edit content_current edit_btn" title="编辑"></i>' +
                        '<i class="Del content_current delete_btn" title="删除"></i>' +
                        '<i class="See content_current pri_btn" title="权限修改"></i>' +
                        '</td>' +
                        '</tr>';
                    $(partsForm).appendTo("#pageTable");
                }
                //3 绑定编辑按钮
                $('.edit_btn').click(function(){
                    edit_func(this);
                });
                $('.delete_btn').click(function(){
                    delete_func(this);
                });
                $('.pri_btn').click(function(){
                    pri_func(this);
                });
                //处理footer 分页
                var totalCount = jsons.pageData.totalCount;
                var pageSize = jsons.pageData.pageSize;
                var totalPage = jsons.pageData.totalPage;
                var pageNo = jsons.pageData.pageNo;
                dealFooterPage(totalCount, pageSize, totalPage, pageNo);
            }
        }
    });
}
